package flash.display
{
	import flash.events.MouseEvent;

	public class InteractiveObject extends DisplayObject
	{
		public function InteractiveObject()
		{
			super();
		}
		protected var _mouseEnabled:Boolean = true;
		public function set mouseEnabled(value:Boolean):void
		{
			_mouseEnabled = value;
			changePropertys["mouseEnabled"] = value;
		}
		public function get mouseEnabled():Boolean
		{
			return _mouseEnabled;
		}
		public var doubleClickEnabled:Boolean = false;

		protected override function init():void
		{
			var GameObject:Type = importType("UnityEngine.GameObject");
			var renderObject:GameObject = new GameObject();
			renderObject.name = this+"("+renderObject.GetInstanceID()+")";
			var RectTransform:Type = importType("UnityEngine.RectTransform");
			renderer = renderObject.AddComponent(RectTransform);
			var Vector2:Type = importType("UnityEngine.Vector2");
			renderer.pivot = new Vector2(0,1);
			renderer.anchorMin = new Vector2(0,1);
			renderer.anchorMax = new Vector2(0,1);
			renderer.SetParent(parent.renderer,false);
			_width = renderer.sizeDelta.x;
			_height = renderer.sizeDelta.y;
		}
		protected override function onAddedToStage():void
		{
			super.onAddedToStage();
			init();
			var EventTriggerListener:Type = importType("flash.events.EventTriggerListener");
			var eventTrigger:EventTriggerListener = renderer.gameObject.AddComponent(EventTriggerListener);
			eventTrigger.AddOnClick(EventTriggerListener.EventDelegate(onClick));
			eventTrigger.AddOnDown(EventTriggerListener.EventDelegate(onDown));
			eventTrigger.AddOnUp(EventTriggerListener.EventDelegate(onUp));
			eventTrigger.AddOnEnter(EventTriggerListener.EventDelegate(onEnter));
			eventTrigger.AddOnExit(EventTriggerListener.EventDelegate(onExit));
			eventTrigger.enabled = mouseEnabled;
		}
		protected function onEnter(eventData:PointerEventData):void
		{
			dispatchEvent(getMouseEventData(MouseEvent.ROLL_OVER,eventData));
		}
		protected function onExit(eventData:PointerEventData):void
		{
			dispatchEvent(getMouseEventData(MouseEvent.ROLL_OUT,eventData));
		}
		protected function onDown(eventData:PointerEventData):void
		{
			var type:String = MouseEvent.MOUSE_DOWN;
			var PointerEventData:Type = importType("UnityEngine.EventSystems.PointerEventData");
			if(eventData.button == PointerEventData.InputButton.Right)
			{
				type = MouseEvent.RIGHT_MOUSE_DOWN;
			}
			if(eventData.button == PointerEventData.InputButton.Middle)
			{
				type = MouseEvent.MIDDLE_MOUSE_DOWN;
			}
			dispatchEvent(getMouseEventData(type,eventData));
		}
		protected function onUp(eventData:PointerEventData):void
		{
			var type:String = MouseEvent.MOUSE_UP;
			var PointerEventData:Type = importType("UnityEngine.EventSystems.PointerEventData");
			if(eventData.button == PointerEventData.InputButton.Right)
			{
				type = MouseEvent.RIGHT_MOUSE_UP;
			}
			if(eventData.button == PointerEventData.InputButton.Middle)
			{
				type = MouseEvent.MIDDLE_MOUSE_UP;
			}
			dispatchEvent(getMouseEventData(type,eventData));
		}
		protected function onClick(eventData:PointerEventData):void
		{
			var type:String = MouseEvent.CLICK;
			var PointerEventData:Type = importType("UnityEngine.EventSystems.PointerEventData");
			if(eventData.button == PointerEventData.InputButton.Right)
			{
				type = MouseEvent.RIGHT_CLICK;
			}
			if(eventData.button == PointerEventData.InputButton.Middle)
			{
				type = MouseEvent.MIDDLE_CLICK;
			}
			dispatchEvent(getMouseEventData(type,eventData));
			if(eventData.clickCount == 2 && doubleClickEnabled)
			{				
				if(eventData.button == PointerEventData.InputButton.Right)
				{
					type = MouseEvent.RIGHT_DOUBLE_CLICK;
				}
				if(eventData.button == PointerEventData.InputButton.Middle)
				{
					type = MouseEvent.MIDDLE_DOUBLE_CLICK;
				}
				dispatchEvent(getMouseEventData(type,eventData));
			}
		}
		private function getMouseEventData(type:String,eventData:PointerEventData):MouseEvent
		{
			var mouseEvent:MouseEvent = new MouseEvent(type);
			mouseEvent.type = type;
			mouseEvent.stageX = eventData.position.x;//暂时代替，有错误的
			mouseEvent.stageY = eventData.position.y;//暂时代替，有错误的
			mouseEvent.localX = eventData.position.x;//暂时代替，有错误的
			mouseEvent.localY = eventData.position.y;//暂时代替，有错误的
			mouseEvent.clickCount = eventData.clickCount;
			return mouseEvent;
		}
		protected override function onRemovedFromStage():void
		{
			var EventTriggerListener:Type = importType("flash.events.EventTriggerListener");
			var eventTrigger:EventTriggerListener = renderer.gameObject.GetComponent(EventTriggerListener);
			eventTrigger.RemoveOnClick(EventTriggerListener.EventDelegate(onClick));
			eventTrigger.RemoveOnDown(EventTriggerListener.EventDelegate(onDown));
			eventTrigger.RemoveOnUp(EventTriggerListener.EventDelegate(onUp));
			eventTrigger.RemoveOnEnter(EventTriggerListener.EventDelegate(onEnter));
			eventTrigger.RemoveOnExit(EventTriggerListener.EventDelegate(onExit));
			super.onRemovedFromStage();
		}
		public override function render():void
		{
			renderMouseEnabled();
			super.render();
		}
		protected function renderMouseEnabled():void
		{
			if(changePropertys["mouseEnabled"])
			{
				var EventTriggerListener:Type = importType("flash.events.EventTriggerListener");
				var eventTrigger:EventTriggerListener = renderer.gameObject.GetComponent(EventTriggerListener);
				eventTrigger.enabled = mouseEnabled;
			}
		}
	}
}